﻿namespace ManagerClinic.DanhMuc
{
    using System;
    using System.Collections.Generic;
    using System.Windows.Forms;
    using ManagerClinic.Modules;
    using System.Linq;
    using ManagerClinic.Core.Business;
    using ManagerClinic.Core.Common;
    using ManagerClinic.Ultilities.Ultility;


    public partial class frmDmPhanLoaiVt : DevExpress.XtraEditors.XtraForm
    {
        IList<DmPhanLoaiVatTuEntity> lstDmPhanLoaiVatTuEntity = null;
        DmPhanLoaiVatTuBusiness dmPhanLoaiVatTuBusiness = new DmPhanLoaiVatTuBusiness();
        DmLoaiVatTuBusiness dmLoaiVatTuBusiness = new DmLoaiVatTuBusiness();
        GridCheckMarksSelection gridCheckMarksSelection = null;

        public frmDmPhanLoaiVt()
        {
            InitializeComponent();
            gridCheckMarksSelection = new GridCheckMarksSelection(gvDmPhanLoaiVatTu);
            LoadLoaiVt();
        }

        private void LoadLoaiVt()
        {
            var lstDmLoaiVTEntity = dmLoaiVatTuBusiness.GetDmLoaiVatTuList();
            var dmloaivtEntity = new DmLoaiVatTuEntity()
            {
                MaLoaiVatTu = ConstantManagerClinic.sValueMember,
                TenLoaiVatTu = ConstantManagerClinic.sDisplayMember
            };
            lstDmLoaiVTEntity.Insert(0, dmloaivtEntity);

            lkLoaiVT.Properties.DataSource = lstDmLoaiVTEntity;
            lkLoaiVT.Properties.DisplayMember = "TenLoaiVatTu";
            lkLoaiVT.Properties.ValueMember = "MaLoaiVatTu";
            lkLoaiVT.EditValue = ConstantManagerClinic.sValueMember;
        }

        void LoadData(object sender, EventArgs e)
        {
            lstDmPhanLoaiVatTuEntity = dmPhanLoaiVatTuBusiness.GetDmPhanLoaiVatTuList(lkLoaiVT.EditValue.ToString());
            grdDmPhanLoaiVatTu.DataSource = lstDmPhanLoaiVatTuEntity;
        }

        private void frmDMPhanloaiVT_Load(object sender, EventArgs e)
        {
            Authenticate.IsAuthenticateForm(this);
            LoadData(sender, e);
        }

        private void lkLoaiVT_EditValueChanged(object sender, EventArgs e)
        {
            LoadData(sender, e);
        }

        private void btnXoa_Click(object sender, EventArgs e)
        {
            var deleteObjects = gridCheckMarksSelection.selection;

            if (deleteObjects.Count <= 0)
            {
                MessageBox.Show("Bạn chưa chọn bản ghi để xóa!", ConstantManagerClinic.sTitleInformation, MessageBoxButtons.OK);
                return;
            }

            if (MessageBox.Show("Bạn chắc chắn muốn xóa dữ liệu?", ConstantManagerClinic.sTitleWarming, MessageBoxButtons.YesNo) == DialogResult.No)
            {
                gridCheckMarksSelection.ClearSelection();
                return;
            }

            foreach (DmDichVuEntity dm in deleteObjects)
            {
                dmPhanLoaiVatTuBusiness.DeleteDmPhanLoaiVatTu(dm.MaDichVu);
            }

            gridCheckMarksSelection.ClearSelection();
            LoadData(sender, e);
        }

        private void btnThemMoi_Click(object sender, EventArgs e)
        {
            var frm = new frmCapNhatDmPhanLoaiVt((int)ConstantManagerClinic.StateInsertOrUpdate.Insert);
            frm.dmPhanLoaiVatTuEvent += new EventHandler(LoadData);
            frm.ShowDialog();

        }

        DmPhanLoaiVatTuEntity getDataDmDichVuEntity()
        {
            if (gvDmPhanLoaiVatTu.RowCount > 0)
            {
                return (DmPhanLoaiVatTuEntity)gvDmPhanLoaiVatTu.GetFocusedRow();
            }

            return null;
        }

        private void btnSua_Click(object sender, EventArgs e)
        {
            var dmDichVuEntity = getDataDmDichVuEntity();
            if (dmDichVuEntity == null) return;

            var frm = new frmCapNhatDmPhanLoaiVt((int)ConstantManagerClinic.StateInsertOrUpdate.Update, dmDichVuEntity);
            frm.dmPhanLoaiVatTuEvent += new EventHandler(LoadData);
            frm.ShowDialog();
        }

        private void btnDong_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void gvPLoaiVT_CustomDrawRowIndicator(object sender, DevExpress.XtraGrid.Views.Grid.RowIndicatorCustomDrawEventArgs e)
        {
            if (e.Info.IsRowIndicator)
            {
                e.Info.DisplayText = (e.RowHandle + 1).ToString();
                e.Handled = false;
            }
        }

        private void grdPLoaiVT_DoubleClick(object sender, EventArgs e)
        {
            if (btnSua.Enabled == false) return;
            btnSua_Click(sender, e);
        }
    }
}